package slu.appli.wodtimer.swing;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.JFrame;

import slu.appli.sound.impl.SoundClip;
import slu.appli.sound.interfaces.SoundPlayerInterface;
import slu.appli.wodtimer.WodTimer;

public class XWodTimer {
	private JFrame frame = null;
	private WodTimer wodTimer = null;
	private SoundPlayerInterface player = null;

	public XWodTimer() {
		super();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		XWodTimer appli = new XWodTimer();
		appli.build();
	}

	public static final String getContextFileName(Object obj) {
		String fileName = System.getProperty("user.home");
		if (fileName == null) return null;
		fileName += File.separator + "WodTimer" + File.separator;
		File file = new File(fileName);
		if (!file.exists()) file.mkdirs();
		fileName += obj.getClass().getName() + ".ctx";
		return fileName;
	}
	
	private void build() {
		SimpleFrame frm = new SimpleFrame();
		this.wodTimer = new WodTimer();
		this.player = new SoundClip("slu/ressources/sound/files/beep.wav",frm.getLogger());
		frm.addWindowListener(new InnerWindowAdapter());
		frm.setWodtimer(this.wodTimer);
		frm.setPlayer(this.player);
		frm.build();
		this.frame = frm;
	}
	
	private void end() {
		this.player.close();
	}
	
	private class InnerWindowAdapter
	extends WindowAdapter {

		@Override
		public void windowClosed(WindowEvent arg0) {
			super.windowClosed(arg0);
			XWodTimer.this.end();
		}

		@Override
		public void windowClosing(WindowEvent arg0) {
			super.windowClosing(arg0);
			XWodTimer.this.frame.dispose();
		}
		
	}
}
